import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from qiskit_ibm_runtime.fake_provider import FakeGuadalupeV2
from utils.core.benchmark import BenchmarkCircuit  # Fixed import to use BenchmarkCircuit instead of Benchmark
from qiskit_aer import AerSimulator
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager

# Initialize the fake backend
backend = FakeGuadalupeV2()

# Create a benchmark circuit for the backend
benchmark_fake = BenchmarkCircuit(num_qubits=2,  # Using 2 qubits for simplicity
                                 qubit_indices=[0])  # Using first two qubits

# Generate basis circuits
benchmark_circuits_fake = benchmark_fake.generate_basis_circuits()

# Create a pass manager for the backend
pm = generate_preset_pass_manager(optimization_level=1, backend=backend)

# Transpile the circuits
transpiled_circuits_fake = pm.run(benchmark_circuits_fake)

# Run the circuits on the fake backend
shots = 1000
job_fake = backend.run(transpiled_circuits_fake, shots=shots)
result_fake = job_fake.result()

# Get the counts for each circuit
fake_results = []
for i in range(len(benchmark_circuits_fake)):
    counts_dict = result_fake.get_counts(i)
    fake_results.append(counts_dict)

# Generate the transition matrix
transition_matrix_fake = benchmark_fake.generate_transition_matrix(fake_results)
print("Transition matrix from fake backend:")
print(transition_matrix_fake)
